//
//  HomeViewController.m
//  体育商城
//
//  Created by  on 2022/9/24.
//

#import "HomeViewController.h"
#import "HomeHeader.h"
#import "HomeCell.h"
#import "SearchViewController.h"

@interface HomeViewController ()<UITextFieldDelegate>
@property (strong, nonatomic) HomeHeader *header;
@property (assign, nonatomic) CGFloat cellH;
@property (strong, nonatomic) IBOutlet BaseTextField *search;
@property (nonatomic, strong) NSArray <Goods*>*array;
@end

@implementation HomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self loadData];
    [self initUI];
}
- (void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
    [self.view bringSubviewToFront:self.search];
}
- (UIStatusBarStyle)preferredStatusBarStyle{
    return UIStatusBarStyleLightContent;
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.search endEditing:YES];
    [self.navigationController setNavigationBarHidden:YES animated:NO];

}
- (void)initUI{
    self.title = LS(@"首页");
    self.backBtn.hidden = YES;
    UIImageView *image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"搜索"]];
    image.frame = CGRectMake(16, 10, 24, 24);
    self.search.leftView = image;
    self.search.leftViewMode = UITextFieldViewModeAlways;
    self.search.backgroundColor = [UIColor colorHex:@"#000000" alpha:0.6];
    self.header = [HomeHeader loadFromXIB];
    self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectZero];
    [self.tableView registerNib:[HomeCell nib] forCellReuseIdentifier:[HomeCell ID]];
    NSLayoutConstraint *top = [self.tableView a1:NSLayoutAttributeTop e:NSLayoutRelationEqual t:self.view a2:NSLayoutAttributeTop c: 0];
    NSLayoutConstraint *left = [self.tableView a1:NSLayoutAttributeLeft e:NSLayoutRelationEqual t:self.view a2:NSLayoutAttributeLeft c:0];
    NSLayoutConstraint *right = [self.tableView a1:NSLayoutAttributeRight e:NSLayoutRelationEqual t:self.view a2:NSLayoutAttributeRight c:0];
    NSLayoutConstraint *bottom = [self.tableView a1:NSLayoutAttributeBottom e:NSLayoutRelationEqual t:self.view a2:NSLayoutAttributeBottom c:0];
    [self.view addConstraints:@[top,left,right,bottom]];
}
- (void)loadData{
    self.array = [GoodsTool find:@"随机" count:arc4random()%10 + 2];
    [self.tableView reloadData];
}
#pragma mark - UITextFieldDelegate
- (void)textFieldDidBeginEditing:(UITextField *)textField{
    SearchViewController *vc = [SearchViewController new];
    [self.navigationController pushViewController:vc animated:YES];
}
#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    HomeCell *cell = [tableView dequeueReusableCellWithIdentifier:[HomeCell ID]];
    cell.array = self.array;
    self.cellH = cell.bounds.size.height;
    return cell;
}
#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return self.header.bounds.size.height;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return  0;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, self.header.bounds.size.height)];
    self.header.frame = view.bounds;
    [view addSubview:self.header];
    return view;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return nil;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return self.cellH;
}
@end
